<?php
namespace UserApi\Controller;
use Zend\Mvc\Controller\AbstractRestfulController;
use Zend\View\Model\JsonModel;
class ChangeCourseApiController extends AbstractRestfulController
{
    public function getList()
    {			
		header('Access-Control-Allow-Origin: *');
		$questionsTable=$this->getServiceLocator()->get('Models\Model\QuestionsFactory');
		$questionsList = $questionsTable->getTotalQuestions()->toarray();
		if(count($questionsList)!=0){
			return new JsonModel(array(
				'questionsList' 	=> json_encode($questionsList),
				'value'				=>	1,
			));
		}else{
			return new JsonModel(array(
				'output' => 'no data',
				'value'	 =>	1,
			));
		}	
    }
    public function get($course_id)
    {		
		header('Access-Control-Allow-Origin: *');
		$subjectsTable=$this->getServiceLocator()->get('Models\Model\SubjectsFactory');
		$courseSubjects = $subjectsTable->courseSubjects($course_id)->toarray();
		if(count($courseSubjects)!=0){
			return new JsonModel(array(
				'getsubjects' 	=> json_encode($courseSubjects),
				'value'	=>	1,
			));
		}else{
			return new JsonModel(array(
				'output' => 'no data',
				'value'	=>	0,
			));
		}	
    }
    public function create($data)
    {
		header('Access-Control-Allow-Origin: *');	
    }
    public function update($course_id,$data)
    {
		header('Access-Control-Allow-Origin: *');	
		$questionTable=$this->getServiceLocator()->get('Models\Model\QuestionsFactory');
		$answerTable=$this->getServiceLocator()->get('Models\Model\AnswersFactory');
		if($data['question_id']==""){
			$saveQuestions  = $questionTable->saveQuestions($course_id,$data);
			$saveAnswer		= $answerTable->saveAnswer($saveQuestions,$data);
		}else{
			$saveQuestions	= $questionTable->upadteQuestions($course_id,$data);
			$saveAnswer		= $answerTable->updateAnswer($saveQuestions,$data);
		}
		
		if($saveQuestions!=0){
			return new JsonModel(array(
				'questionId' 	=> $saveQuestions,
				'value'			=>	1,
			));
		}else{
			return new JsonModel(array(
				'output' => 'no data',
				'value'	=>	0,
			));
		}	
    }
    public function delete($id)
    {
       
    }
}